package PenTest.alibaba.ACM0421;

import java.math.BigInteger;
import java.util.*;

public class ACM01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        Map<Long, Long> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            String[] line = sc.nextLine().split(" ");
            map.put(Long.parseLong(line[1]), map.getOrDefault(Long.parseLong(line[1]), 0L) + Long.parseLong(line[0]));
        }
        PriorityQueue<Long[]> pq = new PriorityQueue<>(new Comparator<Long[]>() {
            @Override
            public int compare(Long[] o1, Long[] o2) {
                return o2[1].compareTo(o1[1]);
            }
        });
        for (Map.Entry<Long, Long> entry : map.entrySet()) {
            pq.add(new Long[] {entry.getKey(), entry.getValue()});
        }
        Long ans=0L;
        ans+=pq.poll()[1];
        ans+=pq.poll()[1];
        System.out.println(ans);
    }
}
